import {Component, ElementRef, AfterViewInit, Input, OnInit, ViewChild} from '@angular/core';
import {Router} from '@angular/router';

import {PageScrollListenerService} from "../../services/page-scroll-listener.service";

@Component({
  selector: 'app-matrix',
  templateUrl: './matrix.component.html',
  styleUrls: ['./matrix.component.css']
})
export class MatrixComponent implements OnInit, AfterViewInit {

  @ViewChild('contentMain', { read: ElementRef, static: false }) contentMain!: ElementRef;

  ngOnInit(): void {

  }

  ngAfterViewInit(): void {
    this.pageScroller.init(
      this.contentMain.nativeElement,
      this.siderIndexList,
      (activeId) => this.activeIndexId = activeId // 活跃索引更新回调
    );
    // 初始触发一次滚动计算
    this.pageScroller.scrollToSection(this.activeIndexId, 60);
  }

  @Input() currentPageTitle: string = '矩阵';
  siderIndexList = [
    { id: 'section1', name: '1.1矩阵的定义及其基本运算' },
    { id: 'section2', name: '1.2矩阵的逆' },
    { id: 'section3', name: '1.3伴随矩阵' },
    { id: 'section4', name: '1.4初等变换和初等矩阵' },
    { id: 'section5', name: '1.5等价矩阵与矩阵的秩' }
  ];

  activeIndexId: string = 'section1'; // 默认选中第一个索引

  constructor(private router: Router,
              private pageScroller: PageScrollListenerService,) { }

  /**
   * 点击索引跳转
   */
  scrollToSection(targetId: string): void {
    this.pageScroller.scrollToSection(targetId, 60);
    this.activeIndexId = targetId;
  }

}
